package com.csun.dsms.controller;

import com.csun.dsms.comment.core.annotation.Authorization;
import com.csun.dsms.comment.core.annotation.CurrentUser;
import com.csun.dsms.comment.core.annotation.ValidateAnnotation;
import com.csun.dsms.comment.core.support.CsunMessageSource;
import com.csun.dsms.comment.core.utils.wrapper.WrapMapper;
import com.csun.dsms.comment.core.utils.wrapper.Wrapper;
import com.csun.dsms.model.domain.DsmsSchedule;
import com.csun.dsms.model.domain.DsmsScheduleLog;
import com.csun.dsms.model.domain.DsmsUser;
import com.csun.dsms.model.dto.schedule.ScheduleLogQueryDto;
import com.csun.dsms.model.dto.schedule.ScheduleQueryDto;
import com.csun.dsms.model.dto.user.ResetPwdDto;
import com.csun.dsms.service.DsmsScheduleService;
import com.github.pagehelper.PageInfo;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author chisj chisj@foxmail.com
 * @version 1.0.0
 * @date 2021-09-23 15:33
 * @description: 调度器管理器拦截器
 */
@RestController
@RequestMapping("/schedule")
public class DsmsScheduleController {

    @Resource
    private CsunMessageSource csunMessageSource;
    @Resource
    private DsmsScheduleService dsmsScheduleService;

    /**
     * 查询调度器列表 - 分页
     *
     * @param scheduleQueryDto  the schedule query dto
     *
     * @return  the wrapper
     */
    @Authorization
    @PostMapping(value = "/queryListWithPage")
    public Wrapper queryListWithPage(@RequestBody ScheduleQueryDto scheduleQueryDto) {

        List<DsmsSchedule> scheduleList = dsmsScheduleService.queryListWithPage(scheduleQueryDto);

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, csunMessageSource
                .getMessage("operation.success", null), new PageInfo<>(scheduleList));
    }

    /**
     * 启动调度器
     *
     * @param scheduleCode  the schedule code
     *
     * @return  the wrapper
     */
    @Authorization
    @PostMapping(value = "/start")
    public Wrapper start(String scheduleCode) {

        boolean result = dsmsScheduleService.start(scheduleCode, null);

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, csunMessageSource
                .getMessage("operation.success", null), result);
    }

    /**
     * 停止调度器
     *
     * @param scheduleCode  the schedule code
     *
     * @return  the wrapper
     */
    @Authorization
    @PostMapping(value = "/stop")
    public Wrapper stop(String scheduleCode) {

        boolean result = dsmsScheduleService.stop(scheduleCode);

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, csunMessageSource
                .getMessage("operation.success", null), result);
    }

}
